/**
 * Date工具类
 */
class DateUtils {
    public constructor() {
    }

    /**
     * 根据秒数格式化字符串
     * @param second 秒数
     * @param type 1:00:00:00   2:yyyy-mm-dd h:m:s    3:00:00   4:xx天前，xx小时前，xx分钟前 12: xx时xx分xx秒
     * @return
     *
     */
    public getFormatBySecond(second: number, type: number = 1): any {
        var str: any = "";
        switch (type) {
            case 1:
                str = this.getFormatBySecond1(second);
                break;
            case 2:
                str = this.getFormatBySecond2(second);
                break;
            case 3:
                str = this.getFormatBySecond3(second);
                break;
        }
        return str;
    }

    //1: 00:00:00
    private getFormatBySecond1(t: number = 0): string {
        var hourst: number = Math.floor(t / 3600);
        var hours: string;
        if (hourst == 0) {
            hours = "00";
        } else {
            if (hourst < 10)
                hours = "0" + hourst;
            else
                hours = "" + hourst;
        }
        var minst: number = Math.floor((t - hourst * 3600) / 60);
        var secondt: number = Math.floor((t - hourst * 3600) % 60);
        var mins: string;
        var sens: string;
        if (minst == 0) {
            mins = "00";
        } else if (minst < 10) {
            mins = "0" + minst;
        } else {
            mins = "" + minst;
        }
        if (secondt == 0) {
            sens = "00";
        } else if (secondt < 10) {
            sens = "0" + secondt;
        } else {
            sens = "" + secondt;
        }
        return hours + ":" + mins + ":" + sens;
    }

    //3: 00:00
    private getFormatBySecond3(t: number = 0): string {
        var hourst: number = Math.floor(t / 3600);
        var minst: number = Math.floor((t - hourst * 3600) / 60);
        var secondt: number = Math.floor((t - hourst * 3600) % 60);
        var mins: string;
        var sens: string;
        if (minst == 0) {
            mins = "00";
        } else if (minst < 10) {
            mins = "0" + minst;
        } else {
            mins = "" + minst;
        }
        if (secondt == 0) {
            sens = "00";
        } else if (secondt < 10) {
            sens = "0" + secondt;
        } else {
            sens = "" + secondt;
        }
        return mins + ":" + sens;
    }

    //2:yyyy-mm-dd h:m:s
    private getFormatBySecond2(time: number): string {
        var date: Date = new Date(time * 1000);
        var year: number = date.getFullYear();
        var month: number = date.getMonth() + 1; 	//返回的月份从0-11；
        var day: number = date.getDate();
        var hours: number = date.getHours();
        var minute: number = date.getMinutes();
        var second: number = date.getSeconds();

        var endHour = hours > 9 ? hours : "0" + hours;
        var endMinute = minute > 9 ? minute : "0" + minute;
        var endSecond = second > 9 ? second : "0" + second;

        return year + "-" + month + "-" + day + " " + endHour + ":" + endMinute + ":" + endSecond;
    }

    /** 
     * 判断是否是同一天
     * @param {number} aTime 单位为秒
     * @param {number} bTime 单位为秒
     */
    public isSameDay(aTime: number, bTime: number): boolean {
        const oneDay = 86400;

        let aDate = new Date(aTime * 1000);
        let bDate = new Date(bTime * 1000);

        let diffSeconds = Math.abs(bTime - aTime);

        let aDay = aDate.getDay();
        let bDay = bDate.getDay();

        if (aDay === bDay && diffSeconds > oneDay) {
            return false;
        }

        if (aDay !== bDay) {
            return false;
        }

        return true;
    }
    
    private static _instance: DateUtils;
    public static getIt(): DateUtils {
        if (!this._instance)
            this._instance = new DateUtils();

        return this._instance;
    }
}